home *** CD-ROM | disk | FTP | other *** search
- // MainView.cpp : implementation of the CMainView class
- //
- // From MFC\OLE\OCLIENT
- //
- ///////////////////////////////////////////////////////////////
- // How to realise automation FindGraph.
- // 1. Create IFindGraph wrapper class from a type FindGraph.tlb.
- // 2. Add #include "FindGraph.h".
- //
- // 3. Add to CRectItem:
- // LPDISPATCH GetIDispatch();
- // This returns IDispatch* for embedded application
- // see sample from MSDN: HOWTO: IQ: Q184663 for details.
- //
- // 4. Call FindGraphs's methods as wrapper:
- // DECLARE_FINDGRAPHS(FindGraph);
- // FindGraph.ReleaseDispatch();
- //
- //
- //
- //
- //
- //
- ///////////////////////////////////////////////////////////////
- //
- // How to use Crov
- // 1. Run Crov.exe.
- // 2. Push button "Insert FindGraph"
- // or select menu item <Edit><Insert FindGraph>.
- // 3. FindGraph plot will appear as embedded object.
- // 4. Deactivate it. To do this click on Crov work area.
- // 5. Message Box will appear: Document was not saved. Save changes?
- // Select to save.
- // 6. Activate FindGraph embedded object.
- // To do this click once on FindGraph object.
- // 9. Toolbar FindGraph buttons will become enabled.
- //10. Push button "Add new points"
- // or select menu item <FindGraph><Add points>.
- //
- ///////////////////////////////////////////////////////////////
-
-
- #include "stdafx.h"
- #include "Crov.h"
-
- #include "MainFrm.h"
- #include "MainDoc.h"
- #include "RectItem.h"
- #include "MainView.h"
-
- #include "FindGraph.h"
- #include <math.h>
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CMainView
- CBrush NEAR CMainView::m_brHatch;
- CLIPFORMAT CMainView::m_cfObjectDescriptor=NULL;
-
-
-
- IMPLEMENT_DYNCREATE(CMainView, CScrollView)
-
- BEGIN_MESSAGE_MAP(CMainView, CScrollView)
- //{{AFX_MSG_MAP(CMainView)
- ON_WM_DESTROY()
- ON_WM_SETFOCUS()
- ON_WM_SIZE()
- ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
- ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
- ON_WM_LBUTTONDBLCLK()
- ON_WM_LBUTTONDOWN()
- ON_WM_SETCURSOR()
- ON_WM_CREATE()
- ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
- ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
- ON_COMMAND(ID_EDIT_CUT, OnEditCut)
- ON_COMMAND(ID_EDIT_PASTE, OnPaste)
- ON_COMMAND(ID_OBJECT_RESETSIZE, OnObjectResetsize)
- ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateEditMenu)
- ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
- ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
- ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
- ON_WM_RBUTTONDOWN()
- ON_COMMAND(ID_EDIT_PASTE_INVIEW, OnEditPasteInview)
- ON_WM_MOUSEMOVE()
- ON_COMMAND(ID_VIEW_NET, OnViewNet)
- ON_UPDATE_COMMAND_UI(ID_VIEW_NET, OnUpdateViewNet)
- ON_COMMAND(ID_INSERT_FINDGRAPH, OnInsertFindGraph)
- ON_COMMAND(ID_ITEM_DOTS, OnItemDots)
- ON_UPDATE_COMMAND_UI(ID_ITEM_DOTS, OnUpdateItemDots)
- ON_COMMAND(ID_ITEM_DOTSGET, OnItemDotsget)
- ON_COMMAND(ID_ITEM_FINDGRAPH, OnItemFindGraphProp)
- ON_UPDATE_COMMAND_UI(ID_ITEM_DOTSGET, OnUpdateItemDots)
- ON_UPDATE_COMMAND_UI(ID_ITEM_FINDGRAPH, OnUpdateItemDots)
- ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_INVIEW, OnUpdateEditPaste)
- ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditMenu)
- ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditMenu)
- ON_UPDATE_COMMAND_UI(ID_OBJECT_RESETSIZE, OnUpdateEditMenu)
- ON_COMMAND(ID_INSERT_EXCEL, OnInsertExcel)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMainView construction/destruction
-
- CMainView::CMainView()
- {
- if (m_brHatch.m_hObject == NULL)
- m_brHatch.CreateHatchBrush(HS_DIAGCROSS, RGB(0,0,0));
-
- if (m_cfObjectDescriptor == NULL)
- m_cfObjectDescriptor =
- (CLIPFORMAT)::RegisterClipboardFormat(_T("Object Descriptor"));
-
- m_bIsNet = true;
- m_pSelection = NULL;
- m_prevDropEffect = DROPEFFECT_NONE;
- m_bInDrag = FALSE;
- m_dwIdDots = -1;
- }
-
- CMainView::~CMainView()
- {
- }
-
- BOOL CMainView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CScrollView::PreCreateWindow(cs);
- }
-
- void CMainView::OnDestroy()
- {
- // Deactivate the item on destruction; this is important
- // when a splitter view is being used.
- CScrollView::OnDestroy();
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
- {
- pActiveItem->Deactivate();
- ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
- }
- }
-
- void CMainView::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
-
-
- // We can't pass MM_ANISOTROPIC to SetScrollSizes so we have to convert to MM_TEXT
- CSize size = GetDocument()->GetDocumentSize();
- CClientDC dc(NULL);
- size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
- size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
- SetScrollSizes(MM_TEXT, size);
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CMainView drawing
- void CMainView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
- {
- CScrollView::OnPrepareDC(pDC, pInfo);
- // set up a reasonable default context
- pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
- pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
-
- // LOENGLISH units are based on physical inches
- // We want logical inches so we have to do it differently
- pDC->SetMapMode(MM_ANISOTROPIC);
- pDC->SetViewportExt(
- pDC->GetDeviceCaps(LOGPIXELSX), pDC->GetDeviceCaps(LOGPIXELSY));
- pDC->SetWindowExt(100,-100);
- }
-
- void CMainView::SetupTracker(CRectTracker* pTracker, CRectItem* pItem,
- CRect* pTrueRect)
- {
- ASSERT(pTracker != NULL);
- ASSERT(pItem != NULL);
-
- pTracker->m_rect = pItem->GetRect();
- DocToClient(pTracker->m_rect);
-
- // set minimum size for our OLE items
- pTracker->m_sizeMin.cx = 8;
- pTracker->m_sizeMin.cy = 8;
-
- pTracker->m_nStyle = 0;
-
- // setup resize handles if item is selected
- if (pItem == m_pSelection)
- pTracker->m_nStyle |= CRectTracker::resizeInside;
-
- // put correct border depending on item type
- if (pItem->GetType() == OT_LINK)
- pTracker->m_nStyle |= CRectTracker::dottedLine;
- else
- pTracker->m_nStyle |= CRectTracker::solidLine;
-
- // put hatching over the item if it is currently open
- if (pItem->GetItemState() == COleClientItem::openState ||
- pItem->GetItemState() == COleClientItem::activeUIState)
- {
- pTracker->m_nStyle |= CRectTracker::hatchInside;
- }
-
- if (pTrueRect != NULL)
- pTracker->GetTrueRect(pTrueRect);
- }
- void CMainView::OnDraw(CDC* pDC)
- {
- CMainDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
-
-
- // TODO: add draw code for native data here
- // TODO: also draw all OLE items in the document
- if (!pDC->IsPrinting())
- {
- m_brHatch.UnrealizeObject();
- CPoint point(0, 0);
- pDC->LPtoDP(&point);
- pDC->SetBrushOrg(point.x % 8, point.y % 8);
-
- CRect rcClip;
- GetClientRect(&rcClip);
- ClientToDoc(rcClip);
- CSize docSize = pDoc->GetDocumentSize();
- if (rcClip.right > docSize.cx)
- {
- CRect rcFill(rcClip);
- rcFill.left = max(rcFill.left,docSize.cx);
- pDC->FillRect(rcFill,&m_brHatch);
- }
- if (rcClip.bottom < -docSize.cy)
- {
- CRect rcFill(rcClip);
- rcFill.top = min(rcFill.top, -docSize.cy);
- pDC->FillRect(rcFill,&m_brHatch);
- }
- if (m_bIsNet)
- {
- CPen pen(PS_DOT, 1, RGB(128,128,128));
- CPen *pen0 = pDC->SelectObject(&pen);
- double dx=100/2.54,
- dy=100/2.54;
- int x, y, i;
- for (i=0, x=1; x < docSize.cx; i++)
- {
- x=1+(int)(dx*i);
- pDC->MoveTo(x, 0);
- pDC->LineTo(x, -docSize.cy);
- }
- for (i=0, y=-1; y>-docSize.cy; i++)
- {
- y=-1-(int)(dy*i);
- pDC->MoveTo(0, y);
- pDC->LineTo(docSize.cx, y);
- }
- pDC->SelectObject(pen0);
- }
- }
-
- // Draw all the CRectItems
- POSITION pos = pDoc->GetStartPosition();
- while (pos != NULL)
- {
- CRectItem* pItem = DYNAMIC_DOWNCAST(CRectItem, pDoc->GetNextItem(pos));
- if (pItem != NULL)
- {
- pItem->Draw(pDC, pItem->GetRect());
-
- if (!pDC->IsPrinting())
- {
- // draw the tracker
- CRectTracker tracker;
- CRect rectTrue;
- SetupTracker(&tracker, pItem, &rectTrue);
- ClientToDoc(rectTrue);
- if (pDC->RectVisible(&rectTrue))
- tracker.Draw(pDC);
- }
- }
- }
- }
-
-
- void CMainView::InvalidateItem(CRectItem* pItem)
- {
- if (m_nMapMode != 0)
- {
- CRectTracker tracker;
- CRect rect;
- SetupTracker(&tracker, pItem, &rect);
- InvalidateRect(&rect);
- }
- }
-
-
-
-
- /////////////////////////////////////////////////////////////////////////////
- // Hit detection, moving and resizing items
-
- CRectItem* CMainView::GetHitItem(CPoint point)
- {
- CMainDoc* pDoc = GetDocument();
- CRectItem* pItemHit = NULL;
-
- // Find the item hit by the mouse
- POSITION pos = pDoc->GetStartPosition();
- while (pos != NULL)
- {
- CRectItem* pItem = DYNAMIC_DOWNCAST(CRectItem, pDoc->GetNextItem(pos));
- if (pItem != NULL)
- {
- CRectTracker tracker;
- SetupTracker(&tracker, pItem);
- if (tracker.HitTest(point) >= 0)
- {
- pItemHit = pItem;
- // items later in the list are drawn on top - so keep looking
- }
- }
- }
- return pItemHit;
- }
-
- void CMainView::DocToClient(CRect& rect)
- {
- CClientDC dc(this);
- OnPrepareDC(&dc);
- dc.LPtoDP(&rect); // convert logical rect to device rect
- rect.NormalizeRect();
- }
-
- void CMainView::ClientToDoc(CRect& rect)
- {
- CClientDC dc(this);
- OnPrepareDC(&dc);
- dc.DPtoLP(&rect); // convert device rect to logical rect
- }
-
- void CMainView::DocToClient(CSize& size)
- {
- CClientDC dc(this);
- OnPrepareDC(&dc);
- dc.LPtoDP(&size); // convert logical size to device size
- size.cx = abs(size.cx);
- size.cy = abs(size.cy);
- }
-
- void CMainView::ClientToDoc(CSize& size)
- {
- CClientDC dc(this);
- OnPrepareDC(&dc);
- dc.DPtoLP(&size); // convert device rect to logical rect
- size.cx = abs(size.cx);
- size.cy = abs(size.cy);
- }
-
- void CMainView::DocToClient(CPoint& point)
- {
- CClientDC dc(this);
- OnPrepareDC(&dc);
- dc.LPtoDP(&point); // convert logical point to device point
- }
-
- void CMainView::ClientToDoc(CPoint& point)
- {
- CClientDC dc(this);
- OnPrepareDC(&dc);
- dc.DPtoLP(&point); // convert device point to logical point
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // Selection support
-
- BOOL CMainView::IsSelected(const CObject* pDocItem) const
- {
- return (pDocItem == m_pSelection);
- }
-
- void CMainView::SetSelection(CRectItem* pNewSel, BOOL bSafeSelect)
- {
- if (pNewSel != NULL && pNewSel == m_pSelection)
- return;
-
- // deactivate any in-place active item on this view!
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL && pNewSel != pActiveItem)
- {
- if (bSafeSelect)
- return;
- // if we found one, deactivate it
- pActiveItem->Close();
- ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
- }
- if (m_pSelection != NULL) // invalidate the old item
- InvalidateItem(m_pSelection);
- if ((m_pSelection = pNewSel) != NULL) // invalidate the new item
- InvalidateItem(m_pSelection);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- void CMainView::UpdateActiveItem()
- {
- // when there is an active item visible, sizing the window may cause
- // more/less of the in-place object to become visible.
- // (ie. the clipping rectangle changes with the size of the window)
- // a container supporting scrolling would also have to do this
- // when scrolling the contents of the window.
-
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL &&
- pActiveItem->GetItemState() == COleClientItem::activeUIState &&
- pActiveItem->GetActiveView() == this)
- {
- // this will update the item rectangles by calling
- // OnGetPosRect & OnGetClipRect.
- pActiveItem->SetItemRects();
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // OLE Client support and commands
-
- void CMainView::OnInsertObject()
- {
- COleInsertDialog dlg;
- if (dlg.DoModal() != IDOK)
- return;
-
- BeginWaitCursor();
-
- CRectItem* pItem = NULL;
- TRY
- {
- // create item from dialog results
- pItem = GetDocument()->CreateItem();
- if (!dlg.CreateItem(pItem))
- AfxThrowMemoryException(); // any exception will do
-
- // try to get initial presentation data
- pItem->UpdateLink();
- pItem->UpdateExtent();
-
- // if insert new object -- initially show the object
- if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
- pItem->DoVerb(OLEIVERB_SHOW, this);
-
- SetSelection(pItem);
- }
- CATCH_ALL(e)
- {
- // cleanup item, if allocated
- if (pItem != NULL)
- GetDocument()->DeleteItem(pItem);
-
- AfxMessageBox(IDP_FAILED_TO_CREATE);
- }
- END_CATCH_ALL
-
- EndWaitCursor();
- }
-
- // The following command handler provides the standard keyboard
- // user interface to cancel an in-place editing session. Here,
- // the container (not the server) causes the deactivation.
- void CMainView::OnCancelEditCntr()
- {
- // Close any in-place active item on this view.
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL)
- {
- pActiveItem->Close();
- }
- ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
- }
-
- void CMainView::OnRButtonDown(UINT nFlags, CPoint point)
- {
- // make sure window is active
- GetParentFrame()->ActivateFrame();
-
- SetSelection(GetHitItem(point)); // reselect item if appropriate
- UpdateWindow();
-
- UINT nID = ID_OBJECT_POPUP_MENU;
- if (m_pSelection == NULL)
- {
- nID = ID_VIEW_POPUP_MENU;
- m_pointMouse = point;
- ClientToDoc(m_pointMouse);
- }
- CMenu bar;
- if (bar.LoadMenu(nID))
- {
- CMenu& popup = *bar.GetSubMenu(0);
- ASSERT(popup.m_hMenu != NULL);
-
- ClientToScreen(&point);
- popup.TrackPopupMenu(TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
- }
- }
-
-
- // Special handling of OnSetFocus and OnSize are required for a container
- // when an object is being edited in-place.
- void CMainView::OnSetFocus(CWnd* pOldWnd)
- {
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL &&
- pActiveItem->GetItemState() == COleClientItem::activeUIState)
- {
- // need to set focus to this item if it is in the same view
- CWnd* pWnd = pActiveItem->GetInPlaceWindow();
- if (pWnd != NULL)
- {
- pWnd->SetFocus();
- return;
- }
- }
- CScrollView::OnSetFocus(pOldWnd);
- }
-
- void CMainView::OnSize(UINT nType, int cx, int cy)
- {
- CScrollView::OnSize(nType, cx, cy);
- UpdateActiveItem();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMainView diagnostics
-
- #ifdef _DEBUG
- void CMainView::AssertValid() const
- {
- CScrollView::AssertValid();
- }
-
- void CMainView::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
-
-
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainView printing
-
- BOOL CMainView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
-
- void CMainView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
-
- void CMainView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainView message handlers
-
-
-
- void CMainView::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- // Double click will activate the main verb
- if (m_pSelection != NULL)
- {
- BeginWaitCursor();
- LONG iVerb = OLEIVERB_PRIMARY;
- if (GetKeyState(VK_CONTROL) < 0)
- iVerb = OLEIVERB_OPEN;
- m_pSelection->DoVerb(iVerb, this);
- EndWaitCursor();
- }
- CScrollView::OnLButtonDblClk(nFlags, point);
- }
-
- void CMainView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CRectItem* pItemHit = GetHitItem(point);
- SetSelection(pItemHit);
- if (pItemHit == NULL)
- return;
-
- CRect rectLimit;
- GetClientRect(rectLimit);
-
- CRectTracker tracker;
- SetupTracker(&tracker, pItemHit);
-
- UpdateWindow(); // update before entering the tracker
-
- if (tracker.HitTest(point) == CRectTracker::hitMiddle) // moving, not sizing
- {
- // determine mouse position offset from the item itself
- CRect rect = pItemHit->GetRect();
- DocToClient(rect);
- CPoint ptOffset(point.x - rect.left, point.y - rect.top);
-
- // determine sensitivity rectangle (determines when drag starts)
- CRect rectDrag(rect.left, rect.top, rect.left+1, rect.top+1);
- // execute the drag/drop operation
- m_bInDrag = TRUE;
- ClientToScreen(&rect); // must be in screen co-ordinates
- ClientToScreen(&rectDrag);
- DROPEFFECT dropEffect = pItemHit->DoDragDrop(rect, ptOffset,
- TRUE, DROPEFFECT_COPY|DROPEFFECT_MOVE, &rectDrag);
- //TRACE1("dropEffect=%d\n",dropEffect);
- if (m_bInDrag == FALSE) return;// move in same window
-
- m_bInDrag = FALSE;
-
- if (dropEffect == DROPEFFECT_MOVE)
- {
- // the item was moved (essentially a copy w/delete)
- pItemHit->Invalidate();
- if (m_pSelection == pItemHit)
- m_pSelection = NULL;
- GetDocument()->DeleteItem(pItemHit);
- }
- }
- else
- if (tracker.Track(this, point))
- {
- ClientToDoc(tracker.m_rect);
- pItemHit->Move(tracker.m_rect);
- GetDocument()->SetModifiedFlag();
- }
-
- CScrollView::OnLButtonDown(nFlags, point);
- }
-
-
-
-
- BOOL CMainView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- if (pWnd == this && m_pSelection != NULL)
- {
- // give the tracker for the selection a chance
- CRectTracker tracker;
- SetupTracker(&tracker, m_pSelection);
- if (tracker.SetCursor(this, nHitTest))
- return TRUE;
- }
- return CScrollView::OnSetCursor(pWnd, nHitTest, message);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // support for drag/drop
- void CMainView::DoPasteNative(
- COleDataObject* pDataObject, CPoint* pPoint, CRectItem* pItem)
- {
- // get file refering to clipboard data
- CFile* pFile = pDataObject->GetFileData(CMainDoc::m_cfPrivate);
- if (pFile == NULL)
- {
- // if the file failed to open, throw an exception
- // to force cleanup in DoPasteItem. the exact
- // type of exception thrown here is unimportant...
-
- AfxThrowFileException(CFileException::generic);
- }
-
- CArchive ar(pFile, CArchive::load);
- TRY
- {
- // connect the file to an archive and read the data
- ar.m_pDocument = GetDocument(); // for COleClientItem serialize
- pItem->Serialize(ar);
- }
- CATCH_ALL(e)
- {
- ar.Close();
- delete pFile;
- THROW_LAST();
- }
- END_CATCH_ALL
-
- ar.Close();
- delete pFile;
-
- // adjust position to that specified by point
- if (pPoint != NULL)
- pItem->m_ptPos = *pPoint;
- }
-
- void CMainView::DoPasteStandard(BOOL bLink, COleDataObject* pDataObject,
- CPoint* pPoint, CRectItem* pItem, CLIPFORMAT cfFormat)
- {
- if (bLink) // paste link
- {
- if (!pItem->CreateLinkFromData(pDataObject))
- AfxThrowMemoryException(); // any exception will do
- }
- // paste embedded
- else if (!pItem->CreateFromData(pDataObject) &&
- !pItem->CreateStaticFromData(pDataObject, OLERENDER_DRAW, cfFormat))
- {
- AfxThrowMemoryException(); // any exception will do
- }
-
- // copy the current iconic representation
- FORMATETC fmtetc;
- fmtetc.cfFormat = CF_METAFILEPICT;
- fmtetc.dwAspect = DVASPECT_ICON;
- fmtetc.ptd = NULL;
- fmtetc.tymed = TYMED_MFPICT;
- fmtetc.lindex = 1;
- HGLOBAL hObj = pDataObject->GetGlobalData(CF_METAFILEPICT, &fmtetc);
- if (hObj != NULL)
- {
- pItem->SetIconicMetafile(hObj);
- // the following code is an easy way to free a metafile pict
- STGMEDIUM stgMed;
- memset(&stgMed, 0, sizeof(stgMed));
- stgMed.tymed = TYMED_MFPICT;
- stgMed.hGlobal = hObj;
- ReleaseStgMedium(&stgMed);
- }
-
- // set the current drawing aspect
- hObj = pDataObject->GetGlobalData(m_cfObjectDescriptor);
- if (hObj != NULL)
- {
- ASSERT(hObj != NULL);
- // got CF_OBJECTDESCRIPTOR ok. Lock it down and extract size.
- LPOBJECTDESCRIPTOR pObjDesc = (LPOBJECTDESCRIPTOR)GlobalLock(hObj);
- ASSERT(pObjDesc != NULL);
- pItem->SetDrawAspect((DVASPECT)pObjDesc->dwDrawAspect);
- GlobalUnlock(hObj);
- GlobalFree(hObj);
- }
-
- // set top-left based on point of drop
- if (pPoint != NULL)
- pItem->m_ptPos = *pPoint;
-
- // get size from drag/drop operation
- CSize size;
- if (GetObjectInfo(pDataObject, &size, NULL) && size.cx != 0 && size.cy != 0)
- {
- // use size obtained from object instead of default
- size.cx = MulDiv(size.cx, 10, 254);
- size.cy = -MulDiv(size.cy, 10, 254);
- pItem->SetSize(size);
- CSize sizeExtent;
- pItem->GetCachedExtent(&sizeExtent);
- pItem->SetBaseSize(sizeExtent);
- }
- else
- {
- // no extent from CF_OBJECTDESCRIPTOR, use extent from object
- pItem->UpdateExtent();
- }
- }
-
-
- // Helper for paste/pastelink
- //
- // bLink pDataObject pPoint cfFormat
- // EditPaste FALSE NULL(clipboard) NULL(default) 0
- // Drag/Drop TRUE/FALSE X X 0
- // PasteLink TRUE NULL(clipboard) NULL(default) 0
- // PasteSpecial TRUE/FALSE X NULL(default) X
- CRectItem* CMainView::DoPasteItem(BOOL bLink, COleDataObject* pDataObject,
- CPoint* pPoint, CLIPFORMAT cfFormat)
- {
- BeginWaitCursor();
-
- CRectItem* pItem = GetDocument()->CreateItem();
- ASSERT_VALID(pItem);
- BOOL bAllowAdjust = (pPoint == NULL) ? TRUE : FALSE;
-
- // use clipboard data if not doing drag/drop
- COleDataObject clipboardData;
- if (pDataObject == NULL)
- {
- clipboardData.AttachClipboard();
- pDataObject = &clipboardData;
- }
-
- TRY
- {
- if (cfFormat == CMainDoc::m_cfPrivate)
- {
- // if format specified (i.e. PasteSpecial) then use that one
- DoPasteNative(pDataObject, pPoint, pItem);
- }
- else if (!bLink && cfFormat == 0 &&
- pDataObject->IsDataAvailable(CMainDoc::m_cfPrivate))
- {
- // if we're not pasting a link, cfFormat was unspecified,
- // and private format is available use it
- DoPasteNative(pDataObject, pPoint, pItem);
- }
- // otherwise perform a standard paste
- else if (bAllowAdjust)
- {
- CPoint ptDef(10, -10);
- DoPasteStandard(bLink, pDataObject, &ptDef, pItem, cfFormat);
- }
- else
- {
- DoPasteStandard(bLink, pDataObject, pPoint, pItem, cfFormat);
- }
-
- if (bAllowAdjust)
- {
- // allow document to adjust position of item so that it doesn't
- // lay directly over an item of the same size
- // this only occurs if the drop point is not specified
- GetDocument()->AdjustItemPosition(pItem);
- }
-
- }
- CATCH_ALL(e)
- {
- // general cleanup
- TRACE0("failed to embed/link an OLE object\n");
- pItem->Delete();
- pItem = NULL;
- }
- END_CATCH_ALL
-
- // set the selection with bSafeSelect = TRUE
- SetSelection(pItem, TRUE);
-
- // update the document and views
- GetDocument()->SetModifiedFlag();
- GetDocument()->UpdateAllViews(NULL, 0, pItem); // including this view
-
- EndWaitCursor();
-
- return pItem;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // support for drag/drop
- int CMainView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CScrollView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- // register drop target
- m_dropTarget.Register(this);
-
- return 0;
- }
-
- BOOL CMainView::OnDrop(COleDataObject* pDataObject,
- DROPEFFECT dropEffect, CPoint point)
- {
- ASSERT_VALID(this);
-
- // clean up focus rect
- OnDragLeave();
-
- // offset point as appropriate for dragging
- GetObjectInfo(pDataObject, &m_dragSize, &m_dragOffset);
- CClientDC dc(NULL);
- dc.HIMETRICtoDP(&m_dragSize);
- dc.HIMETRICtoDP(&m_dragOffset);
- point -= m_dragOffset;
-
- // if move within the view
- ClientToDoc(point);
- if ((dropEffect & DROPEFFECT_MOVE) && m_bInDrag)
- {
- ASSERT(m_pSelection != NULL);
- m_bInDrag = FALSE; // signal drag code that a move happened
- // set top-left based on point of drop
- CRect rect = m_pSelection->GetRect();
- if (rect.TopLeft() != point) // if moved
- {
- m_pSelection->Move(CRect(point,rect.Size()));
- GetDocument()->SetModifiedFlag();
- }
- }
-
- // check and paste link
- else if ((dropEffect & DROPEFFECT_LINK) && DoPasteItem(TRUE, pDataObject, &point))
- return TRUE;
-
- // paste embedding/static
- else if (DoPasteItem(FALSE, pDataObject, &point))
- return TRUE;
-
- return FALSE;
- }
-
-
- BOOL CMainView::GetObjectInfo(COleDataObject* pDataObject,
- CSize* pSize, CSize* pOffset)
- {
- ASSERT(pSize != NULL);
-
- // get object descriptor data
- HGLOBAL hObjDesc = pDataObject->GetGlobalData(m_cfObjectDescriptor);
- if (hObjDesc == NULL)
- {
- if (pOffset != NULL)
- *pOffset = CSize(0, 0); // fill in defaults instead
- *pSize = CSize(0, 0);
- return FALSE;
- }
- ASSERT(hObjDesc != NULL);
-
- // otherwise, got CF_OBJECTDESCRIPTOR ok. Lock it down and extract size.
- LPOBJECTDESCRIPTOR pObjDesc = (LPOBJECTDESCRIPTOR)GlobalLock(hObjDesc);
- ASSERT(pObjDesc != NULL);
- pSize->cx = (int)pObjDesc->sizel.cx;
- pSize->cy = (int)pObjDesc->sizel.cy;
- if (pOffset != NULL)
- {
- pOffset->cx = (int)pObjDesc->pointl.x;
- pOffset->cy = (int)pObjDesc->pointl.y;
- }
- GlobalUnlock(hObjDesc);
- GlobalFree(hObjDesc);
-
- // successfully retrieved pSize & pOffset info
- return TRUE;
- }
-
- DROPEFFECT CMainView::OnDragEnter(COleDataObject* pDataObject,
- DWORD grfKeyState, CPoint point)
- {
- ASSERT(m_prevDropEffect == DROPEFFECT_NONE);
-
- GetObjectInfo(pDataObject, &m_dragSize, &m_dragOffset);
- CClientDC dc(NULL);
- dc.HIMETRICtoDP(&m_dragSize);
- dc.HIMETRICtoDP(&m_dragOffset);
-
- return OnDragOver(pDataObject, grfKeyState, point);
- }
-
- DROPEFFECT CMainView::OnDragOver(COleDataObject*,
- DWORD grfKeyState, CPoint point)
- {
- point -= m_dragOffset; // adjust target rect by original cursor offset
-
- // check for point outside logical area -- i.e. in hatched region
- // GetTotalSize() returns the size passed to SetScrollSizes
- CRect rectScroll(CPoint(0, 0), GetTotalSize());
-
- CRect rectItem(point,m_dragSize);
- if (rectItem.IsRectEmpty())
- {
- // some apps might have a null size in the object descriptor...
- rectItem.InflateRect(1,1);
- }
- rectItem.OffsetRect(GetDeviceScrollPosition());
-
- DROPEFFECT de = DROPEFFECT_NONE;
- CRect rectTemp;
- if (rectTemp.IntersectRect(rectScroll, rectItem))
- {
- // check for force link
- if ((grfKeyState & (MK_CONTROL|MK_SHIFT)) == (MK_CONTROL|MK_SHIFT))
- de = DROPEFFECT_LINK;
- // check for force copy
- else if ((grfKeyState & MK_CONTROL) == MK_CONTROL)
- de = DROPEFFECT_COPY;
- // check for force move
- else if ((grfKeyState & MK_ALT) == MK_ALT)
- de = DROPEFFECT_MOVE;
- // default -- recommended action is move
- else
- de = DROPEFFECT_MOVE;
- }
-
- if (point == m_dragPoint)
- return de;
-
- // otherwise, cursor has moved -- need to update the drag feedback
- CClientDC dc(this);
- if (m_prevDropEffect != DROPEFFECT_NONE)
- {
- // erase previous focus rect
- dc.DrawFocusRect(CRect(m_dragPoint, m_dragSize));
- }
- m_prevDropEffect = de;
- if (m_prevDropEffect != DROPEFFECT_NONE)
- {
- m_dragPoint = point;
- dc.DrawFocusRect(CRect(point, m_dragSize));
- }
- return de;
- }
-
- void CMainView::OnDragLeave()
- {
- CClientDC dc(this);
- if (m_prevDropEffect != DROPEFFECT_NONE)
- {
- dc.DrawFocusRect(CRect(m_dragPoint,m_dragSize)); // erase previous focus rect
- m_prevDropEffect = DROPEFFECT_NONE;
- }
- }
-
- // for scroll with drop
- BOOL CMainView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
- {
- // remove drag/drop feedback before scrolling
- if (bDoScroll && m_prevDropEffect != DROPEFFECT_NONE)
- {
- CClientDC dc(this);
- dc.DrawFocusRect(CRect(m_dragPoint, m_dragSize));
- // erase previous focus rect
- m_prevDropEffect = DROPEFFECT_NONE;
- }
-
- // do the scroll
- if (!CScrollView::OnScrollBy(sizeScroll, bDoScroll))
- return FALSE;
-
- // update the position of any in-place active item
- if (bDoScroll)
- {
- UpdateActiveItem();
- UpdateWindow();
- }
-
- return TRUE;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // Main 'Edit' menu commands
- void CMainView::OnUpdateEditMenu(CCmdUI* pCmdUI)
- {
- // most Edit menu commands are enabled only if we have a selection
- // and there are no in-place activations for this view
- pCmdUI->Enable(m_pSelection != NULL &&
- GetDocument()->GetInPlaceActiveItem(this) == NULL);
- }
- void CMainView::OnUpdateEditPaste(CCmdUI* pCmdUI)
- {
- // determine if private or standard OLE formats are on the clipboard
- COleDataObject dataObj;
- BOOL bEnable = dataObj.AttachClipboard() &&
- (dataObj.IsDataAvailable(CMainDoc::m_cfPrivate) ||
- COleClientItem::CanCreateFromData(&dataObj));
-
- // enable command based on availability
- pCmdUI->Enable(bEnable);
- }
-
-
- void CMainView::OnUpdateEditClearAll(CCmdUI* pCmdUI)
- {
- CMainDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- BOOL bEnable = (pDoc->GetStartPosition() != NULL);
- pCmdUI->Enable(bEnable);
- }
-
-
- void CMainView::OnEditClear()
- {
- if (m_pSelection != NULL)
- {
- CRectItem* pItem = m_pSelection;
- m_pSelection = NULL;
- GetDocument()->DeleteItem(pItem);
- }
- }
-
-
- void CMainView::OnEditCut()
- {
- ASSERT(m_pSelection != NULL);
- TRY
- {
- m_pSelection->CopyToClipboard(TRUE);
- OnEditClear();
- }
- CATCH_ALL(e)
- {
- AfxMessageBox(IDP_CLIPBOARD_CUT_FAILED);
- }
- END_CATCH_ALL
- }
-
- void CMainView::OnEditCopy()
- {
- ASSERT(m_pSelection != NULL);
- TRY
- {
- m_pSelection->CopyToClipboard(TRUE);
- }
- CATCH_ALL(e)
- {
- AfxMessageBox(IDP_CLIPBOARD_COPY_FAILED);
- }
- END_CATCH_ALL
- }
-
- void CMainView::OnPaste()
- {
- if (DoPasteItem(FALSE, NULL, NULL) == NULL)
- AfxMessageBox(IDP_GET_FROM_CLIPBOARD_FAILED);
- }
-
- void CMainView::OnEditPasteInview()
- {
- if (DoPasteItem(FALSE, NULL, &m_pointMouse) == NULL)
- AfxMessageBox(IDP_GET_FROM_CLIPBOARD_FAILED);
- }
-
-
- void CMainView::OnObjectResetsize()
- {
- ASSERT(m_pSelection != NULL);
- m_pSelection->ResetSize();
- }
-
-
- void CMainView::ClearAll()
- {
- CMainDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- m_pSelection = NULL;
-
- POSITION pos = pDoc->GetStartPosition();
- while (pos != NULL)
- {
- CRectItem* pItem = DYNAMIC_DOWNCAST(CRectItem, pDoc->GetNextItem(pos));
- if (pItem != NULL)
- {
- GetDocument()->DeleteItem(pItem);
- }
- }
-
- }
-
- void CMainView::OnEditClearAll()
- {
- ClearAll();
- }
-
-
-
- void CMainView::OnMouseMove(UINT nFlags, CPoint point)
- {
- CScrollView::OnMouseMove(nFlags, point);
- ClientToDoc(point);
- CString str;
- str.Format("X %d, Y %d",(int)(point.x*0.254), -(int)(point.y*0.254));
- ((CMainFrame*)GetParent())->IndicatorMouse(str);
- }
-
- void CMainView::OnViewNet()
- {
- m_bIsNet = !m_bIsNet;
- Invalidate();
- }
-
- void CMainView::OnUpdateViewNet(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bIsNet);
- }
-
-
- // set new size to item
- // size in 0.01 ∞∞
- //
- void CMainView::SelectionSetSize(CSize &size)
- {
- if (m_pSelection != NULL)
- {
- CSize sizeNew(MulDiv(size.cx, 10, 254), - MulDiv(size.cy, 10, 254));
- m_pSelection->SetSize(sizeNew);
- m_pSelection->SetBaseSize(sizeNew);
- }
- UpdateActiveItem();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- void CMainView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
- {
- if (pHint == NULL && lHint == 0)
- {
- // some sort of clear all
- m_pSelection = NULL;
- }
-
- if (pHint != NULL && pHint->IsKindOf(RUNTIME_CLASS(CRectItem)))
- {
- // just invalidate the one item
- InvalidateItem((CRectItem*)pHint);
-
- // clear selection if pointing to deleted item
- if (lHint == 1 && pHint == m_pSelection)
- {
- // specific case of pHint being deleted
- m_pSelection = NULL;
- }
- }
- else if (lHint != 0)
- {
- // invalidate arbitrary rectangle
- InvalidateRect((CRect*)lHint);
- }
- else
- {
- // complete update
- CScrollView::OnUpdate(pSender, lHint, pHint);
- }
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // Insert new object (use for Excel or FindGraph)
- // x,y - top left pos in 0.01 inch
- // return FALSE if object unregistered
- //
- BOOL CMainView::InsertObject(CLSID clsid, int x, int y)
- {
- BOOL bResult = FALSE;
- BeginWaitCursor();
- CRectItem* pItem = NULL;
- TRY
- {
- CMainDoc* pDoc = GetDocument();
- pItem = new CRectItem(pDoc, x, y);
- ASSERT_VALID(pItem);
-
- if (!pItem->CreateNewItem(clsid))
- AfxThrowMemoryException(); // any exception will do
-
- // make sure we deactivate any active items first.
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL)
- pActiveItem->Deactivate();
-
-
- pItem->DoVerb(OLEIVERB_SHOW, this);
-
- ASSERT_VALID(pItem);
- m_pSelection = pItem; // set selection to last inserted item
- pDoc->UpdateAllViews(NULL);
- bResult = TRUE;
- }
- CATCH_ALL(e)
- {
- if (pItem != NULL)
- {
- ASSERT_VALID(pItem);
- pItem->Delete();
- }
- }
- END_CATCH_ALL
- /*
- CString str;
- pItem->GetUserType(USERCLASSTYPE_FULL, str); TRACE("\nUSERCLASSTYPE_FULL = ");TRACE(str);
- pItem->GetUserType(USERCLASSTYPE_SHORT, str); TRACE("\nUSERCLASSTYPE_SHORT = ");TRACE(str);
- pItem->GetUserType(USERCLASSTYPE_APPNAME, str); TRACE("\nUSERCLASSTYPE_APPNAME = ");TRACE(str);
- */
- return bResult;
- }
-
-
-
-
- #pragma warning( disable : 4305 )
- #pragma warning( disable : 4309 )
-
- // Quick insert FiindGraph (fgr) object
- // use clsidFindGraph,
- //
- static const CLSID clsidFindGraph =
- { 0x5c0cac3e, 0x8202, 0x4491, { 0x8a, 0xd, 0x29, 0x87, 0xea7, 0xed, 0xd7, 0xfe } };
- void CMainView::OnInsertFindGraph()
- {
- if (!InsertObject(clsidFindGraph))
- AfxMessageBox(IDP_FAINDGRAPH_FAILED);
- }
-
-
- // Insert Excel sheet
- //
- void CMainView::OnInsertExcel()
- {
- CLSID clsid;
- if(FAILED(::CLSIDFromProgID(L"Excel.sheet",&clsid)))
- {
- AfxMessageBox(IDP_EXCEL_FAILED);
- return;
- }
-
- // Let's arrange a sheet Excel to the right of FindGraph
- if (InsertObject(clsid, int(1050/2.54), 0))
- {
-
-
- }else
- AfxMessageBox(IDP_EXCEL_FAILED);
-
- }
-
-
-
- ///////////////////////////////////////////////////////////////
- // How to realise automation FindGraph.
- // 1. Create IFindGraph wrapper class from a type FindGraph.tlb
- // 2. Add #include "FindGraph.h"
- //
- // 3. Add to CRectItem:
- // LPDISPATCH GetIDispatch();
- // This returns IDispatch* for embedded application
- // see sample from MSDN: HOWTO: IQ: Q184663 for details
- //
- // 4. Call FindGraphs's methods as wrapper:
- // DECLARE_FINDGRAPHS(FindGraph);
- // FindGraph.ReleaseDispatch();
- //
- //
- //
- //
- //
- //
- ///////////////////////////////////////////////////////////////
- // nColor - color number
- // 0 BLACK
- // 1 BLUE
- // 2 GREEN
- // 3 RED
- // 4 BROWN
- // 5 GRAY
- //
- // nShape - marker
- // 0 RECT (CROSS)
- // 1 TRIA
- // 2 CIRC
- //
- // nWidth - X radius, 0,1 mm
- // fAspect - aspect ratio Y/X
-
- ///////////////////////////////////////////////////////////////
- // IDispatch* for embedded application
- //
- LPDISPATCH CMainView::GetIDispatch()
- {
- if (m_pSelection != NULL)
- return m_pSelection->GetIDispatch();
- return NULL;
- }
-
- //start define--------------------------
- #define DECLARE_FINDGRAPHS(FindGraphs) \
- if (m_pSelection==NULL) return;\
- m_pSelection->DoVerb(OLEIVERB_SHOW, this);\
- LPDISPATCH lpDisp = GetIDispatch();\
- if (lpDisp == NULL) return;\
- IFindGraph FindGraphs;\
- FindGraphs.AttachDispatch(lpDisp);\
- //----------------------------end define
-
-
- // How to add at some points to new series
- //
- void CMainView::FindGraphsSet()
- {
- DECLARE_FINDGRAPHS(FindGraph);
-
- CString str;
- long dwId;
- short nColor = 1,
- nShape = 2,
- nWidth =25;
- double fAspect = 1.;
- CString strName = _T("By one");
-
- // Create new series
- dwId = FindGraph.DotsNew(nColor, nShape, nWidth, fAspect, strName);
- m_dwIdDots = dwId;
- if (dwId >= 0)
- {
- // How to add single point to series with dwId
- int n = 20;
- for (int i=0; i<n; i++)
- {
- double fX = 1. + 0.25*i,
- fY = 7. - 0.35*i,
- fZ = i;
- FindGraph.DotsAddPoint(dwId, fX, fY, fZ);
- // Optionally: paint appearance of each point
- FindGraph.DotsUpdate(dwId);
- }
-
- }
-
- // To add at once some points
- // Create new series
- // dwId - identifier of a series
- dwId = FindGraph.DotsNew(2, 1, 20, 1, _T("At once"));
- m_dwIdDots = dwId;
- if (dwId >= 0)
- {
- int n = 1000;
- int nParam = 3;
- // If nParam==3 (x, y, z), if nParam==2 - x, y only
- // One dimensional array with n*nParam elements zero-based indexing
- SAFEARRAYBOUND bounds[1];
- bounds[0].cElements = n*nParam;
- bounds[0].lLbound = 0;
- // Allocate the memory for the descriptor and the array data
- SAFEARRAY *psa = SafeArrayCreate(VT_R8, 1, bounds);
- if (psa)
- {
- psa->fFeatures = FADF_AUTO|FADF_FIXEDSIZE;
- double *pr;
- SafeArrayAccessData(psa, (void**)&pr);
- // Initialize each element
- int ip=0;
- for (int i = 0; i < n; i++)
- {
- double fi = 2.*3.14159/n*i;
- double fX = 4.+3.*sin(fi),
- fY = 3.+2.*cos(fi),
- fZ = i;
- pr[ip++] = fX;
- pr[ip++] = fY;
- pr[ip++] = fZ;
- }
- SafeArrayUnaccessData(psa);// release lock on array state
- // Create a VARIANT that contains our safearray
- VARIANT var; ZeroMemory(&var, sizeof(var));
- var.vt = VT_UI1 | VT_ARRAY;
- var.parray = psa;
- FindGraph.DotsAddPoints(dwId, nParam, var);
- }
-
- FindGraph.DotsUpdate(dwId);
- }
- FindGraph.ReleaseDispatch();
- }
-
-
- // How to get points selected
- //
- void CMainView::FindGraphsGet()
- {
- DECLARE_FINDGRAPHS(FindGraph);
-
- // Copy selected points (X, Y, Z) and put it on the buffer.
- long n = FindGraph.SelectedGetStart(0);
- if (n)
- {
- if (n<10)
- {
- CDot dot;
- // In cycle we choose points:
- while (FindGraph.SelectedGetDot(&dot.m_fX, &dot.m_fY, &dot.m_fZ)==S_OK)
- {
- // There can be your operatings with points here
- // For example to transfer them to Excel sheet
- TRACE("x=%f y=%f z=%f\n", dot.m_fX, dot.m_fY, dot.m_fZ);
-
- }
- }
- // How to get whole array of selected points at once
- else
- {
- int nParam = 3; // x, y, z
- SAFEARRAYBOUND bounds[1];
- bounds[0].cElements = n*nParam;
- bounds[0].lLbound = 0;
- // Allocate the memory for the descriptor and the array data
- SAFEARRAY *psa = SafeArrayCreate(VT_R8, 1, bounds);
- if (psa)
- {
- psa->fFeatures = FADF_FIXEDSIZE; // NOTE: not FADF_AUTO
- // Create a VARIANT that contains our safearray
- VARIANT var; ZeroMemory(&var, sizeof(var));
- var.vt = VT_UI1 | VT_ARRAY;
- var.parray = psa;
-
- // Fill array with points (X, Y, Z).
- if (FindGraph.SelectedGetDots(nParam, &var)==S_OK)
- {
-
- double *pr;
- SafeArrayAccessData(psa, (void**)&pr);
- int ip=0;
- // If nParam==3 Points by triples (X, Y, Z)
- // If nParam==2 Points by couples (X, Y)
- for (int i = 0; i < n; i++)
- {
- double fX = pr[ip ],
- fY = pr[ip+1],
- fZ = (nParam==3) ? pr[ip+2] : 0;
- ip+=nParam;
- // There can be your operatings with points here
- TRACE("x=%f y=%f\n",fX, fY);
- }
- // Release lock on array state
- SafeArrayUnaccessData(psa);
- }
- }
- // Free buffer memory
- n = FindGraph.SelectedGetStop(0);
- }
- }
-
- FindGraph.ReleaseDispatch();
- }
-
-
- // The example how to change plot properties
- //
- void CMainView::FindGraphsParams()
- {
- // The identifier of a series
- DWORD dwId = m_dwIdDots;
- DECLARE_FINDGRAPHS(FindGraph);
-
- // Item (FindGraph plot) size, 0,01 mm
- CSize size (12000, 8000);
- // Set new size to item
- SelectionSetSize(size);
-
- FindGraph.SetDocWidth (size.cx/10); // 0,1 mm
- FindGraph.SetDocHeight(size.cy/10);
-
- FindGraph.SetDocTitle (_T("From Crov title"));
- FindGraph.SetDocComment(_T("From Crov comment"));
- FindGraph.SetAxeXname (_T("Xname"));
- FindGraph.SetAxeXunit (_T("Xunit"));
- FindGraph.SetAxeXstart(-5.); // In X units
- FindGraph.SetAxeXscale( 2.); // Ratio: X units / sm
- FindGraph.SetAxeYname (_T("Yname"));
- FindGraph.SetAxeYunit (_T("Yunit"));
- FindGraph.SetAxeYstart(-2.); // In Y units
- FindGraph.SetAxeYscale( 2.); // Ratio: Y units / sm
-
-
- if (dwId >= 0)
- {
- CString str;
- short nColor = 1,
- nShape = 2,
- nWidth =25;
- double fAspect = 1.;
-
- // Get color number of points in series
- short nColorGet=0;
- if (FindGraph.DotsColorNumGet(dwId, &nColorGet)==S_OK)
- {
- str.Format("nColorGet=%d",nColorGet);
- //AfxMessageBox(str);
- }
- nColor=nColorGet+1;
- // Set new color number
- FindGraph.DotsColorNumSet(dwId, nColor);
-
-
- // Get number of marker
- short nShapeGet=0;
- if (FindGraph.DotsShapeGet(dwId, &nShapeGet)==S_OK)
- {
- //str.Format("nShapeGet=%d",nShapeGet);
- //AfxMessageBox(str);
- }
- nShape=nShapeGet+1;
- // Set new number of marker
- FindGraph.DotsShapeSet(dwId, nShape);
-
-
- // Get radius of circle point
- short nWidthGet=10;
- if (FindGraph.DotsWidthGet(dwId, &nWidthGet)==S_OK)
- {
- //str.Format("nWidthGet=%d",nWidthGet);
- //AfxMessageBox(str);
- }
- nWidthGet*=2;
- if (nWidthGet > 50)
- nWidthGet = 5;
- // Set new radius of circle point
- FindGraph.DotsWidthSet(dwId, nWidthGet);
-
- // Get aspect ratio: (Height Y) / (Width X);
- double fAspectGet=1;
- if (FindGraph.DotsAspectGet(dwId, &fAspectGet)==S_OK)
- {
- //str.Format("fAspectGet=%f",fAspectGet);
- //AfxMessageBox(str);
- }
- fAspectGet*=2;
- if (fAspectGet > 10.)
- fAspectGet = 0.25;
- // Set new aspect ratio
- FindGraph.DotsAspectSet(dwId, fAspectGet);
-
- // Get title of a series
- CString strNameGet;
- //_bstr_t bstr;
- BSTR bstr = strNameGet.AllocSysString();
- if (FindGraph.DotsNameGet(dwId, &bstr)==S_OK)
- {
- strNameGet = CString(bstr);
- //AfxMessageBox(strNameGet);
- }
- strNameGet = "new name";
- // Set new title of a series
- FindGraph.DotsNameSet(dwId, strNameGet);
-
- // Assigns to connect points with line
- FindGraph.DotsShowLine(dwId, 1);
-
- // Next samples:
- // How to get RGB value for number of color
- // long color = FindGraph.DotsColor(1);
-
- }
- FindGraph.DocUpdate();
- FindGraph.ReleaseDispatch();
- }
-
-
- ////////////////////////////////////////////////////
-
- void CMainView::OnUpdateItemDots(CCmdUI* pCmdUI)
- {
- BOOL bEnable = FALSE;
- if (m_pSelection != NULL)
- {
- CLSID clsid; m_pSelection->GetClassID(&clsid);
- bEnable = (clsid==clsidFindGraph);
- }
- pCmdUI->Enable(bEnable);
- }
-
- void CMainView::OnItemDots()
- {
- FindGraphsSet();
- }
-
- void CMainView::OnItemDotsget()
- {
- FindGraphsGet();
- }
-
-
- void CMainView::OnItemFindGraphProp()
- {
- FindGraphsParams();
- }
-
-
-
-